import { TradeActions } from "../actions";
import {
  ISelectSymbolRequest,
  ISelectSymbolResponse,
  ISelectOrderResponse,
  ISelectCoinListsResponse,
  ISelectActiveResponse,
  ISelectHistoryResponse,
  IAddOrderResponse,
  IDelOrderResponse
} from "../models/trade";

export interface State {
  selectSymbolResponse: ISelectSymbolResponse;
  selectOrderResponse: ISelectOrderResponse;
  selectActiveResponse: ISelectActiveResponse;
  selectHistoryResponse: ISelectHistoryResponse;
  selectCoinListsResponse: ISelectCoinListsResponse;
  AddOrderResponse: IAddOrderResponse;
  DelOrderResponse: IDelOrderResponse;
  pending: boolean;
}

export const initialState: State = {
  selectSymbolResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  selectOrderResponse: {
    code: -1,
    data: null,
    msg: "",
    total: 0
  },
  selectActiveResponse: {
    code: -1,
    data: null,
    msg: "",
    total: 0
  },
  selectHistoryResponse: {
    code: -1,
    data: null,
    msg: "",
    total: 0
  },
  selectCoinListsResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  AddOrderResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  DelOrderResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  pending: false
};

export function reducer(
  state = initialState,
  action: TradeActions.TradeActionsUnion
): State {
  switch (action.type) {
    case TradeActions.TradeActionTypes.SelectSymbol: {
      return {
        ...state,
        pending: true
      };
    }
    case TradeActions.TradeActionTypes.SelectSymbolSuccess: {
      return {
        ...state,
        selectSymbolResponse: action.payload.res,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.SelectSymbolError: {
      return {
        ...state,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.SelectCoinLists: {
      return {
        ...state,
        pending: true
      };
    }
    case TradeActions.TradeActionTypes.SelectCoinListsSuccess: {
      return {
        ...state,
        selectCoinListsResponse: action.payload.res,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.SelectCoinListsError: {
      return {
        ...state,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.SelectOrder: {
      return {
        ...state,
        pending: true
      };
    }
    case TradeActions.TradeActionTypes.SelectOrderSuccess: {
      return {
        ...state,
        selectOrderResponse: action.payload.res,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.SelectOrderError: {
      return {
        ...state,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.SelectActive: {
      return {
        ...state,
        pending: true
      };
    }
    case TradeActions.TradeActionTypes.SelectActiveSuccess: {
      return {
        ...state,
        selectActiveResponse: action.payload.res,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.SelectActiveError: {
      return {
        ...state,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.SelectHistory: {
      return {
        ...state,
        pending: true
      };
    }
    case TradeActions.TradeActionTypes.SelectHistorySuccess: {
      return {
        ...state,
        selectHistoryResponse: action.payload.res,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.SelectHistoryError: {
      return {
        ...state,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.AddOrder: {
      return {
        ...state,
        pending: true
      };
    }

    case TradeActions.TradeActionTypes.AddOrderSuccess: {
      return {
        ...state,
        AddOrderResponse: action.payload.res,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.AddOrderError: {
      return {
        ...state,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.DelOrder: {
      return {
        ...state,
        pending: true
      };
    }

    case TradeActions.TradeActionTypes.DelOrderSuccess: {
      return {
        ...state,
        DelOrderResponse: action.payload.res,
        pending: false
      };
    }

    case TradeActions.TradeActionTypes.DelOrderError: {
      return {
        ...state,
        pending: false
      };
    }

    default: {
      return state;
    }
  }
}

export const symbolLists = (state: State) => state.selectSymbolResponse.data;
